
package neteasefilters.filters;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;

public class CvDOF implements IImageFilter {

    public Image makeRadGrad(int w, int h) {
        int rr = w > h ? h / 2 : w / 2;
        RadialGradient gradient = new RadialGradient(w / 2, h / 2, rr, 0xFFFFFFFF, 0xFF000000,
                android.graphics.Shader.TileMode.CLAMP);
        Paint p = new Paint();
        p.setDither(true);
        p.setShader(gradient);

        Bitmap bitmap = Bitmap.createBitmap(400, 400, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.drawCircle(200, 200, 200, p);

        Image _img = new Image(bitmap);
        _img.updateColorArray();

        return _img;
    }

    public Image Get_Line_Mask(int w, int h, float size, float centerX, float centerY, float angle) {
        centerX = centerX * w;
        centerY = centerY * h;
        int startX, startY, endX, endY;
        size = size * (w > h ? h : w);

        if (angle == 90) {
            startX = endX = (int)centerX;
            startY = (int)(centerY - size);
            endY = (int)(centerY + size);
        } else if (angle == 0) {
            startY = endY = (int)centerY;
            startX = (int)(centerX - size);
            endX = (int)(centerX + size);
        } else {
            startX = (int)(centerX - size * Math.sin(angle * Math.PI / 180));
            startY = (int)(centerY + size * Math.cos(angle * Math.PI / 180));
            endX = (int)(centerX + size * Math.sin(angle * Math.PI / 180));
            endY = (int)(centerY - size * Math.cos(angle * Math.PI / 180));
        }

        int[] _color = new int[4];
        _color[0] = Color.BLACK;
        _color[1] = Color.WHITE;
        _color[2] = Color.WHITE;
        _color[3] = Color.BLACK;

        float[] _pos = new float[4];
        _pos[0] = 0;
        _pos[1] = 0.25f;
        _pos[2] = 0.75f;
        _pos[3] = 1.0f;

        LinearGradient grad = new LinearGradient(startX, startY, endX, endY, _color, _pos,
                Shader.TileMode.CLAMP);
        Paint p = new Paint();
        p.setDither(true);
        p.setShader(grad);

        Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.drawRect(0, 0, w, h, p);

        Image _out = new Image(bitmap);
        _out.updateColorArray();

        return _out;
    }

    private Image Get_Radial_Mask(int w, int h, float centerX, float centerY, float size) {
        centerX = centerX * w;
        centerY = centerY * h;
        size = (w > h ? h : w) * size;

        int[] _color = new int[3];
        _color[0] = Color.WHITE;
        _color[1] = Color.WHITE;
        _color[2] = Color.BLACK;
       
        float[] _pos = new float[3];
        _pos[0] = 0;
        _pos[1] = 0.5f;
        _pos[2] = 1.0f;

        RadialGradient grad = new RadialGradient(w / 2, h / 2, size, _color, _pos,
                android.graphics.Shader.TileMode.CLAMP);
        Paint p = new Paint();
        p.setDither(true);
        p.setShader(grad);

        Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.drawRect(0, 0, w, h, p);

        Image _out = new Image(bitmap);
        _out.updateColorArray();

        return _out;
    }

    @Override
    public Image process(Image imageIn) {
        int w = imageIn.getWidth();
        int h = imageIn.getHeight();

        Image _mask;
        int type = 1;

        if (type == 1) {
            _mask = Get_Line_Mask(w, h, 0.5f, 0.5f, 0.5f, 45);
        } else {
            _mask = Get_Radial_Mask(w, h, 0.5f, 0.5f, 0.5f);
        }

        imageIn.updateColorArray();
        int[] _inpix = imageIn.getColorArray();
        int[] _maskpix = _mask.getColorArray();

        LibCvFilter.AddByMask(_inpix, _maskpix, w, h);
        imageIn.setColorArray(_inpix);
        System.gc();

        return imageIn;
    }
}
